import { AxiosPromise } from "axios";
import { http } from "@/utils/http";
import {
  LoginData,
  LoginResult,
  TenantInfo,
  UserInfo,
  VerifyCodeResult
} from "@/views/login/types";

// pc端固定客户端授权id
const clientId = import.meta.env.VITE_APP_CLIENT_ID;

/**
 * @param data {LoginData}
 * @returns
 */
export function login(data: LoginData): AxiosPromise<LoginResult> {
  const params = {
    ...data,
    clientId: data.clientId || clientId,
    grantType: data.grantType || "password"
  };
  return http.request({
    url: "/auth/login",
    headers: {
      isToken: false,
      isEncrypt: true
    },
    method: "post",
    data: params
  });
}

// 注册方法
export function register(data: any) {
  const params = {
    ...data,
    clientId: clientId,
    grantType: "password"
  };
  return http.request({
    url: "/auth/register",
    headers: {
      isToken: false,
      isEncrypt: true
    },
    method: "post",
    data: params
  });
}

/**
 * 注销
 */
export function logout() {
  return http.request({
    url: "/auth/logout",
    method: "post"
  });
}

/**
 * 获取验证码
 */
export function getCodeImg(): AxiosPromise<VerifyCodeResult> {
  return http.request({
    url: "/auth/code",
    headers: {
      isToken: false
    },
    method: "get",
    timeout: 20000
  });
}

/**
 * 第三方登录
 */
export function callback(data: LoginData): AxiosPromise<any> {
  const LoginData = {
    ...data,
    clientId: clientId,
    grantType: "social"
  };
  return http.request({
    url: "/auth/social/callback",
    method: "post",
    data: LoginData
  });
}

// 获取用户详细信息
export function getInfo(): AxiosPromise<UserInfo> {
  return http.request({
    url: "/system/user/getInfo",
    method: "get"
  });
}

// 获取租户列表
export function getTenantList(): AxiosPromise<TenantInfo> {
  return http.request({
    url: "/auth/tenant/list",
    headers: {
      isToken: false
    },
    method: "get"
  });
}
